package com.example.homework.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.homework.R;
import com.example.homework.bean.LoginInfo;
import com.example.homework.utils.MySQLiteOpenHelper;

public class RegisterActivity extends AppCompatActivity {

    private EditText username1, password1;
    private Button buttonRegister1;
    private MySQLiteOpenHelper mySQLiteOpenHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        username1 = findViewById(R.id.account1);
        password1 = findViewById(R.id.password1);
        buttonRegister1 = findViewById(R.id.login1);
        mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
        buttonRegister1.setOnClickListener(view -> {
            String account = username1.getText().toString();
            String password = password1.getText().toString();
            LoginInfo loginInfo = new LoginInfo(account, password);
            long registerSuccess = mySQLiteOpenHelper.register(loginInfo);
            if (registerSuccess != -1) {
                Toast.makeText(this, "注册成功！", Toast.LENGTH_SHORT).show();
                Intent intentLogin = new Intent(this,LoginActivity.class);
                startActivity(intentLogin);
            } else {
                Toast.makeText(this, "注册失败！", Toast.LENGTH_SHORT).show();
            }
        });
    }

}